home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 030 (1987-08-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 030 (1987-08-15)(Ossowski, Stefan)(DE)(PD).adf / WBrun / WBRun.c < prev    next >
C/C++ Source or Header  |  1978-08-10  |  8KB  |  256 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1986 The Software Distillery.  All Rights Reserved */
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .  | || the authors.                                                    */
  5. /* | o  | ||    Dave Baker     Ed Burnette  Stan Chow    Jay Denebeim        */
  6. /* |  . |//     Gordon Keener  Jack Rouse   John Toebes  Doug Walker         */
  7. /* ======          BBS:(919)-471-6436      VOICE:(919)-469-4210              */ 
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <workbench/workbench.h>
  13. #include <workbench/icon.h>
  14. #include <workbench/startup.h>
  15. #include <libraries/dos.h>
  16. #include <libraries/dosextens.h>
  17.  
  18. int setarg(struct WBArg *, char *, struct Lock *);
  19. void msg(char *, char *);
  20.  
  21. struct IconBase *IconBase;
  22. #define ICON_REV 0
  23.  
  24. void main(argc, argv)
  25. register int argc;
  26. char **argv;
  27. {
  28. register struct WBStartup *WBStartup;
  29. register struct DiskObject *diskobj;
  30. char *torun, nambuf[160];
  31. register int stacksize, i;
  32. struct Process *ourtask;
  33. struct MsgPort *replyport;
  34. register struct Lock *olddir, *lock;
  35.  
  36. /* initialize so our cleanup routine runs fine */
  37. WBStartup = NULL;
  38. IconBase  = NULL;
  39. diskobj   = NULL;
  40. replyport = NULL;
  41. olddir    = NULL;
  42.  
  43. /* running this from workbench is ridiculous so just quit */
  44. if (argc == 0) XCEXIT(-1);
  45.  
  46. /* issue the copyright notice - they can supress it by doing a WBRUN >NIL: */
  47. msg(
  48. "\x9B33mWBRun\x9B0m by John Toebes - Copyright ) 1986 \x9B4mThe Software Distillery\x9B0m\n",
  49. "235 Trillingham Ln, Cary NC 27511   BBS:(919)-471-6436");
  50.  
  51. /* not enough parameters? - give them the usage */
  52. if (argc < 2)
  53.    {
  54.    msg(
  55. "Usage: WBRun icon(s)\nWhere icon(s) is to run as if selected from Workbench\n",
  56. "Note: Use WBRun >NIL: icon(s)   to hide copyright message");
  57.    goto done;
  58.    }
  59.  
  60. /* open the libraries we will need */
  61. if ((IconBase = (struct IconBase *)OpenLibrary(ICONNAME, ICON_REV)) == NULL)
  62.    goto done;
  63.  
  64. /* find ourselves - if this doesn't work it deserves to die a horrible */
  65. /* death so I will not even look at our result */
  66. ourtask = (struct Process *)FindTask(NULL);
  67.  
  68. /* allocate storage for all the arguments on the list */
  69. if ((WBStartup = (struct WBStartup *)
  70.                  AllocMem(sizeof(struct WBStartup), MEMF_CLEAR)) == NULL)
  71.    goto done;
  72.  
  73. if ((WBStartup->sm_ArgList = (struct WBArg *)
  74.                  AllocMem(sizeof(struct WBArg)*argc, MEMF_CLEAR)) == NULL)
  75.    goto done;
  76.  
  77. if ((replyport = (struct MsgPort *)CreatePort("PhoneyWorkbench", 0)) == NULL)
  78.    goto done;
  79.  
  80. /* initialize the remainder of the startup fields */
  81. WBStartup->sm_Message.mn_ReplyPort = replyport;
  82. WBStartup->sm_NumArgs = argc-1;
  83.  
  84. /* run through all the arguments getting locks and names for them */
  85. for (i=1; i<argc; i++)
  86.    if (setarg(&WBStartup->sm_ArgList[i-1], argv[i],
  87.               (struct Lock *)ourtask->pr_CurrentDir))
  88.          goto done;
  89.  
  90. /*-- Load the code that is desired ---*/
  91. /* to do this, first find the program to be run */
  92. olddir = (struct Lock *)CurrentDir( WBStartup->sm_ArgList[0].wa_Lock );
  93.  
  94. if ((diskobj = (struct DiskObject *)
  95.                GetDiskObject( WBStartup->sm_ArgList[0].wa_Name )) == NULL)
  96.    {
  97.    msg("Can't get icon for ", WBStartup->sm_ArgList[0].wa_Name);
  98.    goto done;
  99.    }
  100.  
  101. torun = argv[1];
  102.  
  103. /* once we have the object, look at the tool type */
  104. /* for TOOLS we run the program (argv[0] in our case) */
  105. /* For projects it has the name of the default tool to run with it */
  106. /* all others are to be blown away */
  107. if (diskobj->do_Type == WBPROJECT)
  108.    {
  109.    /* for a project, we need to insert the tool icon as the first tool */
  110.    /* move everything over one */
  111.    movmem(&WBStartup->sm_ArgList[0],&WBStartup->sm_ArgList[1],
  112.               (argc-1)*sizeof(struct WBArg));
  113.    strcpy(nambuf, diskobj->do_DefaultTool);
  114.    torun = nambuf;
  115.  
  116.    if (setarg(&WBStartup->sm_ArgList[0], torun, olddir))
  117.       goto done;
  118.  
  119.    FreeDiskObject(diskobj);
  120.  
  121.    if ((diskobj = (struct DiskObject *)
  122.                   GetDiskObject( WBStartup->sm_ArgList[0].wa_Name )) == NULL)
  123.       {
  124.       msg("Can't get icon for ", WBStartup->sm_ArgList[0].wa_Name);
  125.       goto done;
  126.       }
  127.  
  128.    WBStartup->sm_NumArgs++;
  129.    }
  130.  
  131. /* at this point if it is not a TOOL we are scrod */
  132. if (diskobj->do_Type != WBTOOL)
  133.    {
  134.    msg("Icon is not runnable for ", torun);
  135.    goto done;
  136.    }
  137.  
  138. if ((stacksize = diskobj->do_StackSize) < 4000)
  139.    stacksize = 4000;
  140.  
  141. /* so lets load the segment for the program to run */
  142. if ((WBStartup->sm_Segment = (BPTR)LoadSeg(torun)) == NULL)
  143.    {
  144.    msg("Can't open tool ", torun);
  145.    goto done;
  146.    }
  147.  
  148. /* also we will need to get it going as a process */
  149. if ((WBStartup->sm_Process = (struct MsgPort *)
  150.            CreateProc(torun, 0, WBStartup->sm_Segment, stacksize)) == NULL)
  151.    {
  152.    msg("Can't create process for ", torun);
  153.    goto done;
  154.    }
  155.  
  156. /* we are off and running, pass it the message to get running */
  157. WBStartup->sm_ToolWindow = diskobj->do_ToolWindow;
  158.  
  159. PutMsg(WBStartup->sm_Process, WBStartup);
  160.  
  161. /* when he is done with what he needs, we can blow everything away */
  162. WaitPort(replyport);
  163.  
  164. /* everything is now complete so clean up and go away */
  165. done:
  166. if (replyport != NULL) DeletePort(replyport);
  167. replyport = NULL;
  168.  
  169. if (diskobj != NULL) FreeDiskObject(diskobj);
  170. diskobj = NULL;
  171.  
  172. if (olddir != NULL) CurrentDir(olddir);
  173.  
  174. if (WBStartup != NULL)
  175.    {
  176.    if (WBStartup->sm_Segment != NULL)
  177.       UnLoadSeg(WBStartup->sm_Segment);
  178.    if (WBStartup->sm_ArgList != NULL)
  179.       {
  180.       /* run through and free any locks we may have */
  181.       for (i=0; i<argc; i++)
  182.          {
  183.          lock = (struct Lock *)WBStartup->sm_ArgList[i].wa_Lock;
  184.          if ((lock != NULL) && (lock != (struct Lock *)ourtask->pr_CurrentDir))
  185.             UnLock(lock);
  186.          }
  187.       FreeMem(WBStartup->sm_ArgList,sizeof(struct WBArg)*argc);
  188.       }
  189.    FreeMem(WBStartup, sizeof(struct WBStartup));
  190.    }
  191. WBStartup = NULL;
  192.  
  193. if (IconBase != NULL) CloseLibrary(IconBase);
  194. IconBase = NULL;
  195.  
  196. XCEXIT(0);
  197. }
  198.  
  199. /***********************************************************/
  200. /* given a null terminated name, fill in a WBArg structure */
  201. /* with a lock and a name relative to that lock            */
  202. /***********************************************************/
  203. int setarg(WBArg, name, curdir)
  204. register struct WBArg *WBArg;
  205. char *name;
  206. struct Lock *curdir;
  207. {
  208. register char *p, *lastc;
  209. register unsigned char c;
  210.  
  211. if (name == NULL || !*name) return(1);  /* bad name to use */
  212.  
  213. /* first find the last colon or slash in the name */
  214. lastc = NULL;
  215. for (p = name; *p; p++)
  216.    if (*p == ':' || *p == '/')
  217.       lastc = p+1;
  218.  
  219. /* was there a path delimiter at all ? */
  220. if (lastc == NULL)
  221.    {
  222.    /* no, use the default lock and full name */
  223.    WBArg->wa_Lock = (BPTR)curdir;
  224.    WBArg->wa_Name = name;
  225.    }
  226. else
  227.    {
  228.    /* must get a lock on the right directory and use part of the name */
  229.    if (!*lastc) return(1); /* only a drawer specified */
  230.    WBArg->wa_Name = lastc;
  231.  
  232.    /* when setting a directory, we need to include the delimiter */
  233.    c = *lastc;
  234.    *lastc = NULL;
  235.    if ((WBArg->wa_Lock = (BPTR)Lock(name)) == NULL)
  236.       {
  237.       msg("Cannot open directory:", name);
  238.       return(1);  /* couldn't find the director */
  239.       }
  240.    *lastc++ = c;
  241.    }
  242.  
  243. /* it worked so let them continue on */
  244. return(0);
  245. }
  246.  
  247. void msg(str1, str2)
  248. char *str1, *str2;
  249. {
  250. Write(Output(),str1,strlen(str1));
  251. Write(Output(),str2,strlen(str2));
  252. Write(Output(),"\n", 1);
  253. }
  254.  
  255. void MemCleanup(){}
  256.